// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import {utils} from "../utils/utils";
import {model} from "../web/model/Model";

const {ccclass, property} = cc._decorator;

export enum SignItem {
  SIGNED,
  NOT_SIGN,
  SIGNABLE
}

@ccclass
export default class EverydaySignItem extends cc.Component {

  @property(cc.Sprite)
  private icon: cc.Sprite = null;

  @property(cc.Node)
  private blackNode: cc.Node = null;

  @property(cc.Node)
  private hook: cc.Node = null;

  @property(cc.Label)
  private labelName: cc.Label = null;

  @property(cc.Node)
  private signable: cc.Node = null;

  //index 从0开始
  private _data: { index: number, name: string, icon: string, itemType: number }

  private _state: SignItem;

  private _content: cc.Node = null;

  start() {

  }


  set data(value: { index: number; name: string; icon: string, num: number, itemType: number }) {
    this._data = value;
    this.labelName.string = `${value.name}x${value.num}`;
    utils.setIcon(this.icon, value.icon, value.itemType)
    this.refreshState();
  }

  refreshState() {
    let state = SignItem.NOT_SIGN;
    if (model.user.signCounts > this._data.index) {
      state = SignItem.SIGNED;
    } else if (model.user.signCounts === this._data.index && !model.user.isSignToday) {
      state = SignItem.SIGNABLE
    }
    this.state = state
  }

  get state(): SignItem {
    return this._state;
  }

  set state(state: SignItem) {
    this._state = state;
    switch (state) {
      case SignItem.NOT_SIGN:
        this.blackNode.active = false;
        this.signable.active = false;
        this.hook.active = false;
        this.stopShake()
        break;
      case SignItem.SIGNED:
        this.blackNode.active = true;
        this.signable.active = false;
        this.hook.active = true;
        this.stopShake()
        break;
      case SignItem.SIGNABLE:
        this.blackNode.active = false;
        this.signable.active = true;
        this.hook.active = false
        this.shake();
        break;
    }
  }

  private shake() {
    let shake = () => {
      let onceTime = 0.05;
      let seq = cc.sequence(
        cc.rotateTo(onceTime, -10),
        cc.rotateTo(onceTime * 2, 10),
        cc.rotateTo(onceTime * 2, -10),
        cc.rotateTo(onceTime * 2, 10),
        cc.rotateTo(onceTime, 0),
        cc.delayTime(Math.random() * 2 + 1),
        cc.callFunc(shake)
      );
      this.content.runAction(seq);
    };
    shake()
  }

  stopShake() {
    this.content.stopAllActions()
  }


  get content(): cc.Node {
    if (!this._content) {
      this._content = this.node.getChildByName('img_iconBg')
    }
    return this._content;
  }

}
